package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.utils.RedisUtil;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        //删除缓存
        String key = "dish_" + dishDTO.getCategoryId();
        redisUtil.deleteCache(key);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        dishService.saveWithFlavor(dish, dishFlavors);
        return Result.success();
    }

    /**
     * 分页查询菜品
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询菜品")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("批量删除菜品：{}", ids);
        //删除缓存
        redisUtil.deleteCache( "dish_*");
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishWithFlavorById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getDishWithFlavorById(id);
        return Result.success(dishVO);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> getDishWithFlavorByCategoryId(@RequestParam Long categoryId) {
        log.info("根据分类id查询菜品：{}", categoryId);
        List<DishVO> dishVOList = dishService.getDishWithFlavorByCategoryId(categoryId);
        return Result.success(dishVOList);
    }

    /**
     * 更新菜品和口味
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("更新菜品和口味")
    @PutMapping
    public Result<String> updateDishWithFlavor(@RequestBody DishDTO dishDTO) {
        log.info("更新的菜品为：{}", dishDTO);
        //删除缓存
        redisUtil.deleteCache( "dish_*");
        //分解菜品和口味
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        dishService.updateDishWithFlavor(dish,dishFlavors);
        return Result.success();
    }


    /**
     * 修改菜品起售状态
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改菜品起售状态")
    @PostMapping("/status/{status}")
    public Result<String> updateDishStatus(@PathVariable Integer status, @RequestParam Long id) {
        log.info("修改起售状态的菜品ID: {}", id);
        //删除缓存
        redisUtil.deleteCache( "dish_*");
        dishService.updateDishStatus(status, id);
        return Result.success();
    }
}
